package com.lumacong.websocket.controller;

import com.lumacong.websocket.util.ReadIni;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;

@Controller
@RequestMapping("/data")
public class DataController {

    @RequestMapping("/d")
    public String index() {


        return "index";
    }

    public static void readTxtFile(String filePath) {
        try {
            String encoding = "GBK";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    System.out.println(lineTxt);
                }
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }

    }

    public static void main(String argv[]) {
        Map<String, Object> ini = ReadIni.readIni("/Users/mac/LiuPeng/workspase_stock/daan-socket/src/main/resources/templates/data.txt");
        for(String k : ini.keySet()){
            String o = ini.get(k).toString();
            o = o.replace("{","").replace("}","");
            String[] split = o.split(",");
            for (int i = 0; i < split.length; i++) {
                String a = split[i];
                String[] split1 = a.split("=");
                String oldName = split1[0].trim();
                String newName = split1[1].trim();


                boolean rename = Rename("/Users/mac/LiuPeng/workspase_stock/daan-socket/src/main/resources/templates/db/" + oldName + ".png",
                        "/Users/mac/LiuPeng/workspase_stock/daan-socket/src/main/resources/templates/db/" + newName + ".png");
                System.out.println(rename+"~~"+oldName+"~~"+newName);
            }
        }
    }

    public static boolean Rename(String oldname,String newname){
        /*旧文件名*/
        File file1 = new File(oldname);
        /*新文件名*/
        File file2 = new File(newname);
        /*重命名*/
        return file1.renameTo(file2);
    }

   /* public static void main(String [] args){
        System.out.println("Rename file:"+Rename("/Users/mac/LiuPeng/workspase_stock/daan-socket/src/main/resources/templates/123.txt" ,
                "/Users/mac/LiuPeng/workspase_stock/daan-socket/src/main/resources/templates/456.txt"));
        System.out.println("Rename directory:"+Rename("C:\\hello" , "C:\\world"));
    }*/
}
